package willevaluate.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.lang.ref.WeakReference;
import java.util.HashMap;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import utils.KeyBoard;
import utils.ToastUtils;
import view.MyConfirmDialog;

/**
 * Created by Administrator on 2017/6/9.
 * 专家留言
 */
@ContentView(R.layout.expert_evaluate_ask)
public class ExpertEvaluateAsk extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.ask_back)
    private ImageView ask_back;
    @ViewInject(R.id.ask_cancel)
    private TextView ask_cancel;
    @ViewInject(R.id.ask_content)
    private EditText ask_content;
    @ViewInject(R.id.ask_number)
    private TextView ask_number;
    @ViewInject(R.id.ask_commit)
    private Button ask_commit;

    private String content = "";
    private String orderid = "";
    private MyHandler1 handler;
    private  MyConfirmDialog confirmDialog;

    static class MyHandler1 extends Handler {
        WeakReference<ExpertEvaluateAsk> weakReference;

        public MyHandler1(ExpertEvaluateAsk weakReference) {
            this.weakReference = new WeakReference<>(weakReference);
        }

        @Override
        public void handleMessage(Message msg) {
            ExpertEvaluateAsk activity = weakReference.get();
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    try {
                        JSONObject jsonObject = new JSONObject(String.valueOf(msg.obj));
                        String err_code = jsonObject.getString("err_code");
                        if("0".equals(err_code)){
                            //完成
                            activity.setResult(1);
                            activity.finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderid = getIntent().getStringExtra("orderid");
        handler = new MyHandler1(this);
        initView();
        KeyBoard.showKeyBoard();
    }

    private void initView() {
        ask_back.setOnClickListener(this);
        ask_cancel.setOnClickListener(this);
        ask_commit.setOnClickListener(this);
        ask_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 255) {
                    ask_content.setText(content);
                    ask_content.setSelection(content.length());
                    ToastUtils.shortToast("输入字数已达上限");
                } else {
                    content = s.toString();
                }
                ask_number.setText("" + content.length());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ask_back:
            case R.id.ask_cancel:
                confirm();
                break;
            case R.id.ask_commit:
                commitQuestion();
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        confirm();
    }

    private void confirm(){
        confirmDialog = new MyConfirmDialog(this,"退出将不保存问题","是","否");
        confirmDialog.setConfirmListener(new MyConfirmDialog.ConfirmListener() {
            @Override
            public void confirm() {
                ExpertEvaluateAsk.this.finish();
            }
        });
        confirmDialog.show();
    }

    private void commitQuestion() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("question", content);
        hashMap.put("orderid", orderid);
        new InterNetController(this, Constant.SUBMITMSG, handler, hashMap, 0);
    }
}
